package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.dao.CommodityPurchaseRestrictionDao;
import com.huilian.iotbox.admin.service.CommodityPurchaseRestrictionService;
import com.huilian.iotbox.admin.service.CommodityStoreUpDownService;
import com.huilian.iotbox.data.dto.CommodityPurchaseRestrictionDto;
import com.huilian.iotbox.data.dto.CommodityStoreUpDownDto;
import com.huilian.iotbox.data.enums.CommodityPurchaseRestrictionStatusEnum;
import com.huilian.iotbox.data.enums.CommodityStoreUpDownEnum;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.CommodityPurchaseRestrictionVo;
import com.huilian.iotbox.data.vo.CommodityStoreUpDownVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LinYinDong
 * @date 2024/1/22 9:40
 */
@Service
public class CommodityPurchaseRestrictionServiceImpl implements CommodityPurchaseRestrictionService {
    @Resource
    private CommodityPurchaseRestrictionDao commodityPurchaseRestrictionDao;
    @Resource
    private CommodityStoreUpDownService commodityStoreUpDownService;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;

    @Override
    public Exception commodityPurchaseRestrictionExpireJob() {
        Exception temp = null;
        try {
            //查询套餐限购过期
            List<CommodityPurchaseRestrictionVo> cprv = commodityPurchaseRestrictionDao.commodityPurchaseRestrictionList(CommodityPurchaseRestrictionStatusEnum.NORMAL.getCode());
            if (ObjectUtils.isNotEmpty(cprv) && cprv.size() > 0) {
                for (CommodityPurchaseRestrictionVo commodityPurchaseRestrictionVo : cprv) {
                    //查询上下架表
                    CommodityStoreUpDownDto commodityStoreUpDownDto = new CommodityStoreUpDownDto();
                    commodityStoreUpDownDto.setMerchantStoreId(commodityPurchaseRestrictionVo.getMerchantStoreId());
                    commodityStoreUpDownDto.setCommodityStoreUpDownId(commodityPurchaseRestrictionVo.getCommodityStoreUpDownId());
                    CommodityStoreUpDownVo commodityStoreUpDownVo = commodityStoreUpDownService.getOneCommodityStoreUpDown(commodityStoreUpDownDto);
                    if (!StringUtils.isEmpty(commodityStoreUpDownVo)) {
                        //下架
                        CommodityStoreUpDownDto csudd = new CommodityStoreUpDownDto();
                        BeanUtils.copyProperties(commodityStoreUpDownVo, csudd);
                        commodityStoreUpDownDto.setStatus(CommodityStoreUpDownEnum.OFF_SHELF.getCode());
                        commodityStoreUpDownService.update(commodityStoreUpDownDto);
                        //从新上架
                        csudd.setRetailPrice(commodityPurchaseRestrictionVo.getOriginalPrice());
                        csudd.setStatus(CommodityStoreUpDownEnum.GROUNDING.getCode());
                        commodityStoreUpDownService.insert(csudd);
                    }
                }
                //修改状态 为2
                commodityPurchaseRestrictionDao.updateStatus(cprv, CommodityPurchaseRestrictionStatusEnum.EXPIRE.getCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            temp = e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            CommodityPurchaseRestrictionDto commodityPurchaseRestrictionDto = new CommodityPurchaseRestrictionDto();
            String startTime = DateTimeUtils.getYesterdayStartTime();
            String endTime = DateTimeUtils.getYesterdayEndTime();
            commodityPurchaseRestrictionDto.setStartTime(startTime);
            commodityPurchaseRestrictionDto.setEndTime(endTime);
            errorLogCommonService.writerInfoException("查询套餐限购失败", commodityPurchaseRestrictionDto, e);
        }
        return temp;
    }
}
